#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "EvtHandler.h"

#include "Class.h"
#include "Command.h"
#include "CommandProcessor.h"
#include "ET_stdio.h"

static CommandProcessor cmdp;

//---- EvtHandler --------------------------------------------------------------

NewAbstractMetaImpl(EvtHandler, Object, (T(id_)));

EvtHandler::EvtHandler() : id_(cIdNone)
{
}

EvtHandler::EvtHandler(int id) : id_(id)
{
}

EvtHandler::~EvtHandler()
{
}

int EvtHandler::GetId()
{
	return id_;
}

void EvtHandler::SetId(int i)
{
	id_= i;
}

EvtHandler *EvtHandler::GetNextHandler()
{
	AbstractMethod("GetNextHandler");
	return 0;
}

EvtHandler *EvtHandler::FindNextHandlerOfClass(Class *cla)
{
	register EvtHandler *vp;

	for (vp= this; vp; vp= vp->GetNextHandler())
		if (vp->IsA()->isKindOf(cla))
			return vp;
	return 0;
}

//---- menus -------------------------------------------------------------------

class Menu *EvtHandler::GetMenu()
{
	if (GetNextHandler())
		return GetNextHandler()->GetMenu();
	return 0;
}

void EvtHandler::DoSetupMenu(class Menu *mp)
{
	if (GetNextHandler())
		GetNextHandler()->DoSetupMenu(mp);
	else
		cmdp.DoSetupMenu(mp);
}

Command *EvtHandler::DoMenuCommand(int cmd)
{
	if (GetNextHandler())
		return GetNextHandler()->DoMenuCommand(cmd);
	return gNoChanges;
}

void EvtHandler::PerformCommand(Command *cmd)
{
	if (cmd && cmd != gNoChanges) {
		if (GetNextHandler())
			GetNextHandler()->PerformCommand(cmd);
		else {
			fprintf(stderr, "EvtHandler::PerformCommand\n");
			cmdp.PerformCommand(cmd);
		}
	}
}

//---- firsthandler ------------------------------------------------------------

bool EvtHandler::SetFirstHandler(EvtHandler *eh)
{
	if (GetNextHandler())
		return GetNextHandler()->SetFirstHandler(eh);
	return TRUE;
}

bool EvtHandler::KbdFocus(bool)
{
	return TRUE;
}

//---- input distribution ------------------------------------------------------

Command *EvtHandler::Input(Point, Token&, Clipper*)
{
	return gNoChanges;
}

void EvtHandler::SendDown(int, int, void*)
{
}

void EvtHandler::ExtCommand(int from, char *req, char *args, int len,
							char *&ret, int &retlen)
{
	if (GetNextHandler())
		GetNextHandler()->ExtCommand(from, req, args, len, ret, retlen);
}

void EvtHandler::InputKbd(Token&)
{
}

void EvtHandler::Send(int id, int part, void *val)
{
	Object::Send(id, part, val);
/*
	if (GetNextHandler())
		GetNextHandler()->Send(id, part, val);
*/
	if (GetNextHandler())
		GetNextHandler()->Control(id, part, val);
}

//---- misc --------------------------------------------------------------------

void EvtHandler::Control(int id, int part, void *val)
{
	if (GetNextHandler())
		GetNextHandler()->Control(id, part, val);
}

//---- input/output ------------------------------------------------------------

OStream& EvtHandler::PrintOn(OStream &s)
{
	Object::PrintOn(s);
	return s << id_ SP;
}

IStream& EvtHandler::ReadFrom(IStream &s)
{
	Object::ReadFrom(s);
	return s >> id_;
}

